package com.nuoniu.sibanyun.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.finance.ErpAbstract;
import com.nuoniu.sibanyun.mapper.finance.ErpAbstractMapper;
import com.nuoniu.sibanyun.service.finance.IErpAbstractService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 凭证摘要 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2022-04-24
 */
@Service
public class ErpAbstractServiceImpl extends ServiceImpl<ErpAbstractMapper, ErpAbstract> implements IErpAbstractService {

    @Override
    public List<ErpAbstract> getList() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", UserThreadLocal.get().getCompanyId());
        return list(wrapper);
    }

    @Override
    public void saveAbstract(ErpAbstract erpAbstract) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.eq("content", erpAbstract.getContent());
        wrapper.last("LIMIT 1");
        ErpAbstract oldAbstract = this.getOne(wrapper);
        if(oldAbstract == null) {
            erpAbstract.setCompanyId(userInfo.getCompanyId());
            erpAbstract.setUserId(userInfo.getUserId());
            this.saveOrUpdate(erpAbstract);
        }
    }
}
